﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraCharts;
using StockCommon.data.bid5;
using StockCommon.data.TradeDetail;
using System.Drawing;


namespace StockImageLib
{
    public class StockBid5ImageGenerator
    {
        

        public bool FillStockBid5(ChartControl chart, String code, DateTime date)
        {
            Dictionary<String, object> bidData = StockBid5DataProvider.getData(code, date);
            if (bidData == null)
                return false;
            removeUselessSeries(chart);
            FillTradeData(chart, bidData);



            return false;
        }

        private static void FillTradeData(ChartControl chart, Dictionary<String, object> bidData)
        {
            Series 指數series = chart.Series["指數"];
            Series 成交量Series = chart.Series["成交量"];
            List<StockMinuteDataSlice> tradeData = (List<StockMinuteDataSlice>)bidData[StockBid5DataProvider.交易資料];
            指數series.Points.Clear();
            成交量Series.Points.Clear();


            List<SeriesPoint> ps1 = new List<SeriesPoint>(tradeData.Count);
            List<SeriesPoint> ps2 = new List<SeriesPoint>(tradeData.Count);

            int sz = tradeData.Count;
            for (int i = 0; i < sz ; i++)
            {
                
                StockMinuteDataSlice slice = tradeData[i];
                DateTime dt = slice.time;
                SeriesPoint p1 = new SeriesPoint(dt, new double[] { slice.成交價 });
                SeriesPoint p2 = new SeriesPoint(dt, new double[] { slice.單量 });
                ps1.Add(p1);
                if (i < sz - 1)
                    ps2.Add(p2);
            }
            
            指數series.Points.AddRange(ps1.ToArray());
            成交量Series.Points.AddRange(ps2.ToArray());

            double up = (double)bidData[StockBid5DataProvider.漲停價];
            double down = (double)bidData[StockBid5DataProvider.跌停價];

            DevExpress.XtraCharts.XYDiagram diagram = (XYDiagram)chart.Diagram;

            diagram.AxisY.Range.Auto = false;
            diagram.AxisY.Range.MaxValue = up;
            diagram.AxisY.Range.MinValue = down;
            ConstantLine constantLine1=null;
            if (diagram.AxisY.ConstantLines.Count == 0)
            {
                constantLine1 = new ConstantLine("前日收盤");
                diagram.AxisY.ConstantLines.Add(constantLine1);
                constantLine1.Visible = true;
                constantLine1.Color = Color.Red;
                constantLine1.LineStyle.DashStyle = DashStyle.Dash;
                constantLine1.LineStyle.Thickness = 1;
            }
            else
                constantLine1 = diagram.AxisY.ConstantLines[0];
            constantLine1.AxisValue = (double)bidData[StockBid5DataProvider.前日收盤價];
            


        }

        private static void removeUselessSeries(ChartControl chart)
        {
            int ssz = chart.Series.Count;
            List<String> toRemove = new List<string>();
            for (int i = 0; i < ssz; i++)
            {
                String ss = chart.Series[i].Name;
                toRemove.Add(ss);
            }
            toRemove.Remove("指數");
            toRemove.Remove("成交量");
            toRemove.Remove("單量1");
            toRemove.Remove("單量2");
            foreach (String s in toRemove)
            {
                chart.Series.Remove(chart.Series[s]);
            }
        }
    }
}
